import htw.sortedList.*;
import java.io.IOException;
import exceptions.*;
import htw.stack.*;
import uebString.*;

/**
 * Interaktiver Dialog fuer Uebung 12
 */
 
public class StringDialog {
	
	private StringBuffer hilfeDialog 		= new StringBuffer(); 
	private static char ADD					= 	'+';
	private static char REMOVE				= 	'-';
	private static String REGEX_ADD			= 	"\\+";
	private static String REGEX_REMOVE		=	"\\-";
	private static String NOT_VALID			= 	"No valid operator!";
	private static String INFO				= 	"Syntax\n(+/-)<Dateiname>\thinzufuegen/entfernen aus Liste\n<Dateiname>\tje nach Operator der Zeile hinzufuegen oder entfernen aus der Liste";
	
	private StringStack 		stack 	= new StringStack();
	private SortedStringDList 	list 	= new SortedStringDList();
	
	// Objektinitialsierungsblock
	{
		// Erstellen des Hilfedialoges
		hilfeDialog.append(StringDialogEnum.INFO.getHD());
		hilfeDialog.append(StringDialogEnum.HELP.getHD());
		hilfeDialog.append(StringDialogEnum.END.getHD());
	}
	
	private void trennZeile() {
		for(int i = 0; i <= LAENGE; i++) {
			System.out.print("*");
		}
		System.out.println();
	}
	
	/**
	 * Hauptscheilfe des Programmes
	 */
	public void start() {
		String funktion = null;
		while(!funktion.equalsIgnoreCase(StringDialogEnum.END.getCommand())){
			try{
				command = einlesenFunktion();
				trennZeile();
				runMethod(command);
			} catch(AssertionError e) {
				System.out.println(e);
				e.printStackTrace();
			} catch(Exception e) {
				System.out.println(e);
				e.printStackTrace();
			}    
		}
	}
	
	private void runMethod(String command) {
		if(StringDialogEnum.HELP.getCommand().equalsIgnoreCase(command)) {
			System.out.println(hilfeDialog());
		} else if(StringDialogEnum.INFO.getCommand().equalsIgnoreCase(command)) {
			info();
		} else {
			// weiter mit auswertung und verarbeitung
			mainMethod(command);
		}
	}
	/**
	 * Ausfuehren der Listoperationen
	 */
	private void listOperation(String command) {
		StringReader	reader				= null;
		boolean			theEndIsNear		= false;
		String			contend				= null;	
		int				counter				= 0;
		
		if(command.charAt(1) == ADD){
			try {	
				reader = new StringReader(command.split(REGEX_ADD)[1]);
				
				while(!theEndIsNear) {
					contend = reader.read(counter);
					if(contend == null)
						theEndIsNear = true;
					list.add(contend);
					System.out.println(counter + contend);
					counter ++;
				}
			} catch(NullPointerException npe) {
				System.out.println(npe.getMessage());
			} catch(StringIsEmptyException siee) {
				System.out.println(siee.getMessage());
			} catch(IsNoFileException inf) {
				System.out.println(inf.getMessage());
			} catch(CanNotReadException cnre) {
				System.out.println(cnre.getMessage());
			} catch(IOException io) {
				System.out.println(io.getMessage());
			} catch(AlreadyInListException aile){
				System.out.println(aile.getMessage());
			}
		} else if(command.charAt(1) == REMOVE) {
			try {
				reader = new StringReader(command.split(REGEX_REMOVE)[1]);
				
				while (!theEndIsNear) {
					contend = reader.read(counter);
					if(contend == null)
						theEndIsNear = true;
						
					list.remove(contend);
					System.out.println(counter + contend);
					counter++;
				}
			} catch(NullPointerException npe) {
				System.out.println(npe.getMessage());
			} catch(StringIsEmptyException siee) {
				System.out.println(siee.getMessage());
			} catch(IsNoFileException inf) {
				System.out.println(inf.getMessage());
			} catch(CanNotReadException cnre) {
				System.out.println(cnre.getMessage());
			} catch(IOException io) {
				System.out.println(io.getMessage());
			}
		}
	}
	
	/**
	 * Ausfuehren der Stackoperationen
	 */
	private void stackOperation(String command) {
		StringReader 	reader 				= null;
		boolean 		theEndIsNear 		= false;
		String 			contend	 			= null;
		int 			counter				= 0;
		
		try {
			reader = new StringReader(command);
			
			while(!theEndIsNear) {
				contend = reader.read(counter);
				if(contend.charAt(1) == ADD){
					stack.push(contend.split(REGEX_ADD)[1]);
					System.out.println(counter + contend.split(REGEX_REMOVE)[1]);
				} else if(contend.charAt(1) == REMOVE){
					System.out.println(stack.pop());
				} else {
					System.out.println(NOT_VALID);
					System.out.println(counter + contend);
				}
				if(contend == null)
					theEndIsNear = true;
				counter++;
			}
		} catch(NullPointerException npe) {
			System.out.println(npe.getMessage());
		} catch(StringIsEmptyException siee) {
			System.out.println(siee.getMessage());
		} catch(IsNoFileException inf) {
			System.out.println(inf.getMessage());
		} catch(CanNotReadException cnre) {
			System.out.println(cnre.getMessage());
		} catch(IOException io) {
			System.out.println(io.getMessage());
		}
	}
	
	/**
	 * Entscheided zu welchem Kontainer hinzugefuegt wird
	 *
	 * @param fileName Der eingelesene Dateiname mit Operator
	 */
	private void mainMethod(String fileName) {
		if(fileName.charAt(1) == ADD || fileName.charAt(1) == REMOVE) {
			listOperation(fileName);
		} else {
			stackOperation(fileName);
		}
	}  
	
	/**
	 * Gibt eine Trennzeile aus und fragt nach einer Befehleingabe
	 * 
	 * @return Der eingegebene Befehl
	 */
	private String einlesenFunktion(){
		trennZeile();
		System.out.print("");
		return Stdin.readString();
	}
	
	private void info() {
		System.out.println(INFO);
	}
	private String hilfeDialog() {
		return hilfeDialog.toString();
	}
	
	public static void main(String[] args) {
		System.out.println("Test");
	}
}